package com.xinxin.leetcode.problem443;

import java.util.Arrays;

/**
 * @author ShiXinXin
 * @date 2019-12-16 16:06
 */
public class Solution {
    public int compress(char[] chars) {
        if (chars.length == 0) {
            return 0;
        }
        int count = 1;
        int num = 0;
        StringBuilder res = new StringBuilder();
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] == chars[i - 1]) {
                count++;
            } else {
                if (count == 1) {
                    res.append(chars[i - 1]);
                } else {
                    num += String.valueOf(count).length();
                    res.append(chars[i - 1]).append(String.valueOf(count));
                }
                num++;
                count = 1;
            }
        }

        if (count == 1) {
            res.append(chars[chars.length - 1]);
        } else {
            num += String.valueOf(count).length();
            res.append(chars[chars.length - 1]).append(String.valueOf(count));
        }
        num++;
        for (int i = 0; i < num; i++) {
            chars[i] = res.charAt(i);
        }
        return num;
    }

    public static void main(String[] args) {
        char[] arr = {'a', 'a', 'b', 'b', 'b', 'c'};
        System.out.println(new Solution().compress(arr));
        System.out.println(arr);
    }
}
